{include file='Public/header' /}
<div class="layui-layout layui-layout-admin">
{include file='Public/head' /}	
{include file='Public/side' /}
<div class="layui-body"> 
{include file='Public/crumbs' /}
	<!-- 内容主体区域 -->
	<div class="shu-body">
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12 layui-col-xs12">
					<form class="layui-form" method="post">
						<a href="{:url('add?pid='.input('pid'))}" class="layui-btn"><i class="layui-icon">&#xe61f;</i> 添加</a>
						<table lay-even class="layui-table">
							<colgroup>
							<col>
							</colgroup>
							<thead>
								<tr>
									<th>分类名称</th>
									<th>排序</th>
									<th class="center">操作</th>
								</tr>
							</thead>
							<tbody>
                            {if condition="!empty($category)"}
                                {php}
                                    function showTree($data,$pre=null) {
									$pid = input('param.pid');
                                    foreach ($data as $key => $val) {
                                    echo '
                                    <tr>';
                                        echo '
                                        <td>';
                                            if($pre){
                                            echo $pre.'┗━'.$val['name'];
                                            }else{
                                            echo $val['name'];
                                            }
                                            echo '
                                        </td>
                                        ';
                                        echo '
                                        <td>';
                                            echo '<input style="width:150px;" name="o" class="layui-input inputorder" type="number" value="'.$val['o'].'" val="'.$val['id'].'"/>';
                                            echo '
                                        </td>
                                        ';
                                        echo '
                                        <td>';
                                            
											echo '<a class="layui-btn layui-btn-small" href="'.url('add',array('ppid'=>$val['id'],'pid'=>$pid)).'" title="新增分类">新增</a>&nbsp;&nbsp;';
                                            echo '<a class="layui-btn layui-btn-small" href="'.url('edit',array('id'=>$val['id'],'pid'=>$pid)).'" title="编辑分类">编辑{$pid}</a>&nbsp;&nbsp;';
                                            echo '<a class="layui-btn layui-btn-small layui-btn-danger del_btn" href="javascript:void(0);" nickname="'.$val['name'].'" val="'.$val['id'].'" title="删除分类">删除</a>';
                                            echo '
                                        </td>
                                        ';
                                        if (!empty($val['children'])) {
                                        showTree($val['children'],$pre.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                                        }

                                        echo '
                                    </tr>
                                    ';
                                    }
                                    }
                                    showTree($category);
                                {/php}
                                {else/}
                                <tr>
                                    <td colspan="3">无分类</td>
                                </tr>
                            {/if}
							</tbody>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- 内容主体区域 END--> 
</div>
{include file='Public/footer' /} 
<script>
layui.use('form',function(){
  var form = layui.form,
  jq = layui.$;
	//排序
	jq(".inputorder").change(function () {
		var id = jq(this).attr('val');
		var o = jq(this).val();
		if (id != o) {
			loading = layer.load(2, {
				shade: [0.2,'#000']
	  		});
			jq.post("{:url('update',array('act'=>'order'))}", {id: id, o: o}, function (data) {
				console.log(o);
				if(data.code == 200){
				  layer.close(loading);
				  layer.msg(data.msg, {icon: 1, time: 1000}, function(){
					//location.reload();
				  });
				}else{
				  layer.close(loading);
				  layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
				}
			});
		}
	});
    
	//删除
  	jq('.del_btn').click(function(){
		var name = jq(this).attr('nickname');
    	var id = jq(this).attr('val');
    	layer.confirm('确定删除【'+name+'】栏目?', function(index){
      		loading = layer.load(2, {
        		shade: [0.2,'#000']
      		});
			jq.get("{:url('del')}?id=" + id,function(data){
				if(data.code == 200){
					layer.close(loading);
					layer.msg(data.msg, {icon: 1, time: 1000}, function(){
						location.reload();
					});
				}else if(data.code == 100){
					layer.close(loading);
					layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
				}else if(data.code == 300){
					layer.close(loading);
					layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
				}else if(data.code == 0){
					layer.close(loading);
					layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
				}
			});
    	});   
  	});



})
</script> 
